{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Note on the derivation of the reflection and transmission coefficents in\n",
    "Formulation for stable and efficient implementation of the rigorous coupled-wave analysis of binary gratings\n",
    "M. G. Moharam, Eric B. Grann, Drew A. Pommet, and T. K. Gaylord </br>\n",
    "\n",
    "The final step in this paper seems straightforward but is actually non-trivial to work out to get the final working RCWA code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start with a system of four equations: \n",
    "\n",
    "$\\begin{bmatrix} \n",
    "\\delta_{i0}  \\\\ \n",
    "jn_Icos(\\theta)\\delta_{i0}  \\\\   \n",
    "\\end{bmatrix}$ + \n",
    "$\\begin{bmatrix} \n",
    "I  \\\\ \n",
    "-jY_I \\\\   \n",
    "\\end{bmatrix}$[R] = \n",
    "$\\begin{bmatrix} \n",
    "W  & WX\\\\ \n",
    "V & -VX  \\\\   \n",
    "\\end{bmatrix}$$\\begin{bmatrix} \n",
    "c^{+}  \\\\ \n",
    "c^{-}  \\\\   \n",
    "\\end{bmatrix}$\n",
    "\n",
    "\n",
    "$\\begin{bmatrix} \n",
    "I  \\\\ \n",
    "jY_II \\\\   \n",
    "\\end{bmatrix}$[T] = \n",
    "$\\begin{bmatrix} \n",
    "WX  & W\\\\ \n",
    "VX & -V  \\\\   \n",
    "\\end{bmatrix}$$\\begin{bmatrix} \n",
    "c^{+}  \\\\ \n",
    "c^{-}  \\\\   \n",
    "\\end{bmatrix}$\n",
    "\n",
    "This is the original form written in the paper, but it is more transparent to write them out so you see all four equations\n",
    "\n",
    "$\n",
    "\\delta_{i0} + R = Wc^{+}+WXc^{-} \n",
    "$ <br\\> $\n",
    "jn_{I}cos(\\theta) -jY_IR = Vc^{+}-VXc^{-} \n",
    "$<br\\> $\n",
    "T = WXc^{+} + Wc^{-}\n",
    "$<br\\> $\n",
    "jY_{II} = VXc^{+} - Vc^{-}\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So we have four equations and four unknowns, so we should be able to solve them. What we will do then is to solve $c^+$ and $c^-$ in terms of T using the last two equations and then substitute them out in the first two equations. Then we have a system of two equations in R and T only. Then we simplify one more time to solve R."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we solve for $c^{-}$<br\\>\n",
    "$\n",
    "c^{-} = W^{-1}(T-Xc^{+})\n",
    "$\n",
    "<br\\> Now we can solve $c^{+}$ using the expression above <br\\>\n",
    "$\n",
    "c^{+} = F^{-1}(jY_{II} + VW^{-1})T\n",
    "$\n",
    "<br\\> where the term $F$ is given by: <br\\>\n",
    "$\n",
    "F = V(I+W^{-1})X\n",
    "$\n",
    "<br\\> We can substitute this back into the expression for $c^{-}$ <br\\>\n",
    "$\n",
    "c^{-} = W^{-1}\\bigg(I-XF^{-1}(iY_{II}+VW^{-1})\\bigg)T\n",
    "$\n",
    "<br\\>\n",
    "We will call the expression inside the big parentheses $G$ to keep notation simple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now we mark the steps that substitutes our expressions above into the reflection equations\n",
    "First we rewrite the two reflection equations: <br\\>\n",
    "$\n",
    "\\delta_{i0} + R = Wc^{+}+WXc^{-} \n",
    "$ <br\\> $\n",
    "jn_{I}cos(\\theta) -jY_IR = Vc^{+}-VXc^{-} \n",
    "$<br\\> \n",
    "\n",
    "Now we begin substitution:<br\\>\n",
    "$\n",
    "\\delta_{i0} + R = W\\bigg[F^{-1}(iY_{II}+VW^{-1}) +XG \\bigg]T\n",
    "$ <br\\>\n",
    "$\n",
    "jn_Icos(\\theta)\\delta_{i0} -jY_IR = V\\bigg[ F^{-1}(iY_{II}+VW^{-1}) - XG\\bigg]T\n",
    "$\n",
    "<br\\> Now we do two notation simplifications: <br\\>\n",
    "$\n",
    "A = F^{-1}(iY_{II}+VW^{-1}) +XG \n",
    "$ <br\\>\n",
    "$\n",
    "B =  F^{-1}(iY_{II}+VW^{-1}) - XG\n",
    "$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can finally solve for R: <br\\>\n",
    "$\n",
    "T = A^{-1}(\\delta_{i0} + R)\n",
    "$\n",
    "<br\\>\n",
    "$\n",
    "(jn_Icos(\\theta) - VBA^{-1})\\delta_{i0} = (jY_I + VBA^{-1})R\n",
    "$ <br\\>\n",
    "$\n",
    "R = \\frac{(jn_Icos(\\theta) - VBA^{-1})}{(jY_I + VBA^{-1})}\\delta_{i0} \n",
    "$ "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At this point we are done, we can now backsubstitute our solution for R into T and then finally into $c^{+}$ and $c^{-}$\n",
    "\n",
    "THIS METHOD FAILS: TOO MUCH ILL CONDITIONING"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Potential Failure Case in RCWA with Scattering Matrices\n",
    "\n",
    "consider $a=1$ and $\\lambda=1$ (all in units of microns, $L_0 = 1\\times10^{-6}$ m\n",
    "now consider the calculations of the eigenmodes in a linear homogeneous medium:\n",
    "$\n",
    "P = \\begin{bmatrix} \n",
    "K_XK_y & nI-K_x^2  \\\\ \n",
    "K_y^2 -nI & K_xK_y\\\\   \n",
    "\\end{bmatrix}\n",
    "$\n",
    "\n",
    "where ($n=\\epsilon\\mu$) and $K_x = k_{inc,x}- \\frac{2\\pi n}{a_x}$, $K_y = k_{inc,y}- \\frac{2\\pi m}{a_y}$; and we consider the special case of normal incidence so $k_{inc,x} = k_{inc,y} = 0$\n",
    "\n",
    "The E-field eigenmodes (denoted by W) is the just the identity matrix.\n",
    "the eigenvalues of $\\Omega^2$ are apparently just\n",
    "\n",
    "$\n",
    "P = \\begin{bmatrix} \n",
    "iKz & 0  \\\\ \n",
    "0 & iKz\\\\   \n",
    "\\end{bmatrix}\n",
    "$\n",
    "where:\n",
    "\n",
    "$\n",
    "K_z = \\bigg( \\sqrt{k_0^2n - K_x^2 - K_y^2} \\bigg)^*\n",
    "$\n",
    "\n",
    "THE ISSUE: WE CAN ENGINEER SITUATIONS WHERE KZ IS SINGULAR <br\\>\n",
    "typically, for our gap media, we pick $n_g=1$\n",
    "Additionally, we normalize by $k_0 = \\frac{2\\pi}{\\lambda_0}$\n",
    "\n",
    "This means that when we calculate a normalized $K_x$, we get:\n",
    "$\n",
    "K_x =  \\frac{2\\pi n}{k_0a_x} = n\n",
    "$\n",
    "As a result, we can see that in the $K_z$ matrix, we will have zeros on the diagonal. The main problem this has is in the calculation of the H field modes:\n",
    "\n",
    "$\n",
    "V = QW\\lambda^{-1}\n",
    "$\n",
    "\n",
    "\n",
    "This problem is not just for gap media, but also if any of the layers or reflection/transmission regions have such properties...As a result, it isn't obvious to me that scattering matrix formalism is 'unconditionally stable'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
